package net.board.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.board.db.*;

 public class BoardModifyAction implements Action {
	 public ActionForward execute(HttpServletRequest request,HttpServletResponse response) 
	 	throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		 
		
		 ActionForward forward = new ActionForward();
		 boolean result = false;
		 
		 int number = Integer.parseInt(request.getParameter("number"));
		 int ID=Integer.parseInt(request.getParameter("ID"));
		 
		 BoardDAO boarddao=new BoardDAO();
		 BoardBean boarddata=new BoardBean();
		 
		 boolean usercheck=boarddao.isBoardWriter(ID, request.getParameter("QNA_PASS"));
		 if(usercheck==false){
		   		response.setContentType("text/html;charset=utf-8");
		   		PrintWriter out=response.getWriter();
		   		out.println("<script>");
		   		out.println("alert('パスワードが違います。');");
		   		out.println("location.href='./BoardList.bo?number="+number+"';");
		   		out.println("</script>");
		   		out.close();
		   		return null;
		 }
		 
		 try{
			 boarddata.setID(ID);
			 boarddata.setQNA_NICK(request.getParameter("QNA_NICK"));
			 boarddata.setQNA_SUBJECT(request.getParameter("QNA_SUBJECT"));
			 boarddata.setQNA_CONTENT(request.getParameter("QNA_CONTENT"));
			 
			 result = boarddao.boardModify(boarddata);
			 if(result==false){
		   		return null;
		   	 }
		   	 
		   	 forward.setRedirect(true);
		   	 forward.setPath("./BoardDetailAction.bo?ID="+boarddata.getID()+"&number="+number);
		   	 return forward;
	   	 }catch(Exception ex){
	   			ex.printStackTrace();	 
		 }
		 
		 return null;
	 }
}